package com.dream.keigezhushou.Activity.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.dream.keigezhushou.R;


/**
 * Created by lizhilong on 2016/7/7.
 */

public class DialogBase {

    protected PopupWindow popupWindow = null;
    public static Context context;
    public View contentView;
    public int backColorState;


    /**
     * @param contentView
     * @param context
     * @param height      设置高度
     */
    public DialogBase(View contentView, Context context, int width, int height) {
        this.context = context;
        this.contentView = contentView;

        popupWindow = new PopupWindow(contentView);
        popupWindow.setHeight(height);
        popupWindow.setWidth(width);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(
                R.color.touming));
        popupWindow.setBackgroundDrawable(dw);
        popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        popupWindow.update();
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            public void onDismiss() {

                backGroundAlpha(1f);
            }
        });

    }

    /**
     * 设置背景为半透明
     *
     * @param alp
     */
    public void backGroundAlpha(float alp) {
        WindowManager.LayoutParams lParams = ((Activity) context)
                .getWindow().getAttributes();
        lParams.alpha = alp;
        ((Activity) context).getWindow().setAttributes(lParams);

    }

    public void setPopAnim(int anim) {
        popupWindow.setAnimationStyle(anim);
    }

    public void showPopAtLocation(View view, int gravity) {
        if (!popupWindow.isShowing()) {
            if (backColorState == 0){
                backGroundAlpha(0.4f);
            }
            popupWindow.showAtLocation(view, gravity, 0, 0);
        } else {
            popupWindow.dismiss();
        }
    }

    // dismiss
    public void dismiss() {
        if (popupWindow != null) {
            popupWindow.dismiss();
        }
    }

    public void setBackColorState(int backColorState) {
        this.backColorState = backColorState;
    }
}
